package bb.lanxing.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.baidu.mapapi.clusterutil.clustering.ClusterItem;
import com.baidu.mapapi.clusterutil.clustering.ClusterManager;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.github.mikephil.charting.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.routebook.RouteBookDetailMapActivity;
import bb.lanxing.fragment.base.BaseMapFragment;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.lib.widget.utils.Density;
import bb.lanxing.manager.BaiduLocationManager;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.data.Notepoint;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.database.RouteBookPoint;
import bb.lanxing.model.database.MapPOI;
import bb.lanxing.model.database.Waypoint;
import bb.lanxing.model.json.LatestLocation;
import bb.lanxing.model.map.GeoBounds;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.LocCountry;
import bb.lanxing.util.Log;
import bb.lanxing.util.MapUtil;
import bb.lanxing.util.map.MapConfigs;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BaiduMapFragment extends BaseMapFragment {
    private static final String TAG = "BaiduMapFragment";
    private final CoordinateConverter converter = new CoordinateConverter();
    public OnMapReadyListener onMapReadyListener;
    private TextureMapView mapView;
    private BaiduMap mBaiduMap;
    private ClusterManager<TeamItem> mClusterManager;
    private Marker currentMarker;

    private MapViewListener<TextureMapView, LatLng, Marker, Polyline> mapViewListener;
    private Polyline sportLine;

    private LinkedList<LatLng> workoutPoints = new LinkedList<>();
    private LinkedList<Overlay> workoutOverlays = new LinkedList<>();
    private LinkedList<Overlay> lushuOverlays = new LinkedList<>();
    private LinkedList<Overlay> segmentOverlays = new LinkedList<>();
    private LinkedList<Overlay> naviOverlays = new LinkedList<>();
    private LinkedList<Overlay> altitudeOverlays = new LinkedList<>();
    private LinkedList<Overlay> distanceOverlays = new LinkedList<>();
    private LinkedList<Overlay> paceOverlays = new LinkedList<>();
    private LinkedList<Overlay> poiOverlays = new LinkedList<>();
    private LinkedList<Overlay> slopeOverlays = new LinkedList<>();
    private LinkedList<Subscription> subscriptions = new LinkedList<>();
    private boolean touching = false;
    private float direction = -1.0f;

    public static BaiduMapFragment newInstance(double lat, double lng, boolean drawLoc, float zoom, int locationMode, int type) {
        return newInstance(lat, lng, drawLoc, zoom, locationMode, type, 80);
    }

    public static BaiduMapFragment newInstance(double lat, double lng, boolean drawLoc, float zoom, int locationMode, int type, int compassMargin) {
        BaiduMapFragment baiduMapFragment = new BaiduMapFragment();
        Bundle bundle = new Bundle();
        bundle.putDouble("map_loc_lat", lat);
        bundle.putDouble("map_loc_lng", lng);
        bundle.putFloat("map_zoom_level", zoom);
        bundle.putInt("map_location_mode", locationMode);
        bundle.putInt("map_tile_type", type);
        bundle.putBoolean("map_draw_location", drawLoc);
        bundle.putInt("map_compass_margin", compassMargin);
        baiduMapFragment.setArguments(bundle);
        return baiduMapFragment;
    }

    @Override
    protected int getPoiZIndex(int type) {
        return (type == 1 || type == 2) ? 1 : 0;
    }

    @Override
    protected int getVisiblePOILevel(float zoom) {
        return ((int) zoom) - 2;
    }

    private LatLng earth2Baidu(LatLng earth) {
        if (LocCountry.inChina(earth, true)) {
            this.converter.coord(earth);
            this.converter.from(CoordinateConverter.CoordType.GPS);
            return this.converter.convert();
        }
        return earth;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_baidu_map, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mapView = view.findViewById(R.id.mapView);
        mapView.showZoomControls(false);
        mBaiduMap = mapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setMaxAndMinZoomLevel(4.0f, 21.0f);
        initMapListener();
        mClusterManager = new ClusterManager<>(getActivity(), mBaiduMap);
        mClusterManager.setOnClusterItemClickListener(item -> false);
        Bundle arguments = getArguments();
        if (arguments != null) {
            arguments.getBoolean("enable_scale_bar", true);
            mBaiduMap.setCompassEnable(arguments.getBoolean("enable_compass"));
            compassMargin = arguments.getInt("map_compass_margin", 120);
            adjustCompassPosition(compassMargin);
            double map_loc_lat = arguments.getDouble("map_loc_lat");
            double map_loc_lng = arguments.getDouble("map_loc_lng");
            if (map_loc_lat > Utils.DOUBLE_EPSILON && map_loc_lng > Utils.DOUBLE_EPSILON) {
                moveTo(map_loc_lat, map_loc_lng);
            }
            if (arguments.getBoolean("map_draw_location", false)) {
                setLocationData(new LatLng(map_loc_lat, map_loc_lng), 50.0f, -1.0f);
            }
            float map_zoom_level = arguments.getFloat("map_zoom_level", 17.0f);
            if (map_zoom_level < mBaiduMap.getMinZoomLevel()) {
                map_zoom_level = mBaiduMap.getMinZoomLevel();
            } else if (map_zoom_level > mBaiduMap.getMaxZoomLevel()) {
                map_zoom_level = mBaiduMap.getMaxZoomLevel();
            }
            updateMapStatus(MapStatusUpdateFactory.zoomTo(map_zoom_level), false);
            locationMode = arguments.getInt("map_location_mode", 1);
            if (locationMode == 1) {
                setLocationConfig(MyLocationConfiguration.LocationMode.NORMAL);
            } else if (locationMode == 2) {
                setLocationConfig(MyLocationConfiguration.LocationMode.FOLLOWING);
            } else if (locationMode == 3) {
                setLocationConfig(MyLocationConfiguration.LocationMode.COMPASS);
            }
        }
        super.onViewCreated(view, savedInstanceState);
        if (onMapReadyListener != null) {
            onMapReadyListener.OnMapReady(this);
        }

        BaiduLocationManager.getInstance().setLocationChangedListener(latLng -> {
            Log.i("setLocationChangedListener latitude = " + latLng.latitude + ",longitude = " + latLng.longitude);
            LatLng earth2Baidu2 = BiCiCoorConverter.earth2Baidu(latLng);
            setLocationData(earth2Baidu2, 50.0f, -1.0f);
            moveTo(earth2Baidu2.latitude, earth2Baidu2.longitude);
            if (null != listener) {
                listener.onLocationChanged(latLng);
            }
        });
        BaiduLocationManager.getInstance().closeBaiduGps();
        BaiduLocationManager.getInstance().openBaiduGps();
    }

    private void initMapListener() {
        mBaiduMap.setOnMapTouchListener(event -> {
            int action = event.getAction();
            if (action != 1) {
                if (action == 2) {
                    if (BaiduMapFragment.this.touching) {
                        return;
                    }
                    BaiduMapFragment.this.touching = true;
                    return;
                } else if (action != 3) {
                    Iterator<ViewGroup> it2 = BaiduMapFragment.this.mInterceptTouchView.iterator();
                    while (it2.hasNext()) {
                        it2.next().requestDisallowInterceptTouchEvent(true);
                    }
                    return;
                }
            }
            Iterator<ViewGroup> it3 = BaiduMapFragment.this.mInterceptTouchView.iterator();
            while (it3.hasNext()) {
                it3.next().requestDisallowInterceptTouchEvent(false);
            }
        });
        mBaiduMap.setOnMarkerClickListener(marker -> {
            if (BaiduMapFragment.this.mapViewListener != null) {
                BaiduMapFragment.this.mapViewListener.onMakerClick(marker);
                return true;
            }
            return false;
        });
        mBaiduMap.setOnMapLongClickListener(latLng -> {
            if (BaiduMapFragment.this.mapViewListener != null) {
                BaiduMapFragment.this.mapViewListener.onMapLongClick(BaiduMapFragment.this.mapView, latLng);
            }
        });
        mBaiduMap.setOnPolylineClickListener(polyline -> {
            if (BaiduMapFragment.this.mapViewListener != null) {
                BaiduMapFragment.this.mapViewListener.onPolylineClick(polyline);
                return true;
            }
            return false;
        });
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapPoiClick(MapPoi mapPoi) {
            }

            @Override
            public void onMapClick(LatLng latLng) {
                if (BaiduMapFragment.this.mapViewListener != null) {
                    BaiduMapFragment.this.mapViewListener.onMapClick(BaiduMapFragment.this.mapView, latLng);
                }
            }
        });
        mBaiduMap.setOnMapStatusChangeListener(new AnonymousClass7());
    }

    private void setLocationData(LatLng latLng, float accuracy, float direction) {
        if (mBaiduMap == null) {
            return;
        }
        try {
            mBaiduMap.setMyLocationData(new MyLocationData.Builder().latitude(latLng.latitude).longitude(latLng.longitude).accuracy(accuracy).direction(direction).build());
        } catch (Exception e) {
            Log.w(TAG, "setLocationData: " + e.getMessage());
        }
    }

    private void setLocationConfig(MyLocationConfiguration.LocationMode locationMode) {
        MyLocationConfiguration.LocationMode locationMode2 = this.mBaiduMap.getLocationConfiguration() != null ? this.mBaiduMap.getLocationConfiguration().locationMode : null;
        MyLocationConfiguration myLocationConfiguration = new MyLocationConfiguration(locationMode, true, null);
        if (locationMode == MyLocationConfiguration.LocationMode.COMPASS) {
            adjustCompassPosition(this.compassMargin);
            float f = this.direction;
            if (f >= 0.0f) {
                updateMyLocDirection(f);
            }
        }
        try {
            this.mBaiduMap.setMyLocationConfiguration(myLocationConfiguration);
        } catch (Exception e) {
            Log.w(TAG, "setLocationConfig: exception occur " + e.getMessage());
        }
        if (locationMode2 != MyLocationConfiguration.LocationMode.COMPASS || locationMode == MyLocationConfiguration.LocationMode.NORMAL) {
            return;
        }
        updateMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder(this.mBaiduMap.getMapStatus()).rotate(0.0f).overlook(0.0f).build()), true);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        this.mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!this.subscriptions.isEmpty()) {
            Iterator<Subscription> it2 = this.subscriptions.iterator();
            while (it2.hasNext()) {
                Subscription next = it2.next();
                if (!next.isUnsubscribed()) {
                    next.unsubscribe();
                }
            }
            this.subscriptions.clear();
        }
        this.mapView.onDestroy();
    }

    @Override
    public float getZoomLevel() {
        MapStatus mapStatus;
        BaiduMap baiduMap = this.mBaiduMap;
        if (baiduMap != null && (mapStatus = baiduMap.getMapStatus()) != null) {
            return mapStatus.zoom;
        }
        Bundle arguments = getArguments();
        if (arguments == null) {
            return 17.0f;
        }
        return arguments.getFloat("map_zoom_level", 17.0f);
    }

    @Override
    public float getMaxOrMinZoom(boolean max) {
        BaiduMap baiduMap = this.mBaiduMap;
        if (baiduMap != null) {
            if (max) {
                return baiduMap.getMaxZoomLevel();
            }
            return baiduMap.getMinZoomLevel();
        }
        return 0.0f;
    }

    @Override
    public void zoomIn() {
        updateMapStatus(MapStatusUpdateFactory.zoomIn(), true);
    }

    @Override
    public void zoomOut() {
        updateMapStatus(MapStatusUpdateFactory.zoomOut(), true);
    }

    @Override
    public void moveTo(double lat, double lng) {
        updateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(lat, lng)), true);
    }

    public void updateMapStatus(MapStatusUpdate update, boolean anim) {
        if (anim) {
            try {
                mBaiduMap.animateMapStatus(update);
            } catch (Exception e) {
                Log.w(TAG, "updateMapStatus: " + e.getMessage());
            }
        } else {
            try {
                mBaiduMap.setMapStatus(update);
            } catch (Exception e2) {
                Log.w(TAG, "updateMapStatus: " + e2.getMessage());
            }
        }
    }

    @Override
    public void changeMapType(int type) {
        BaiduMap baiduMap = this.mBaiduMap;
        if (baiduMap == null) {
            return;
        }
        if (type == 0) {
            baiduMap.setMapType(1);
        } else if (type == 1) {
            baiduMap.setMapType(2);
        } else if (type == 3) {
            baiduMap.setMapType(3);
        }
    }

    private void updateMyLocDirection(float direction) {
        MyLocationData locationData = this.mBaiduMap.getLocationData();
        MyLocationData.Builder builder = new MyLocationData.Builder();
        if (locationData != null) {
            builder.latitude(locationData.latitude).longitude(locationData.longitude).accuracy(locationData.accuracy);
        }
        builder.direction(direction);
        try {
            this.mBaiduMap.setMyLocationData(builder.build());
        } catch (Exception e) {
            Log.w(TAG, "setLocationData: " + e.getMessage());
        }
    }

    @Override
    public Object drawLine1(int type, List<LatLng> points, int color, boolean bound) {
        if (points.size() < 2) {
            Log.w(TAG, "drawLine: points is less than 2 ! size = " + points.size());
            return null;
        }
        try {
            Overlay addOverlayList = addOverlayList(type, mBaiduMap.addOverlay(new PolylineOptions().points(points).color(color).width(10)));
            if (bound) {
                MapUtil.displayWithBound(this.mBaiduMap, points);
            }
            return addOverlayList;
        } catch (Exception e) {
            Log.w(TAG, "drawLine: " + e.getMessage());
            return null;
        }
    }

    @Override
    public Object drawLine(int type, List<IGeoPoint> points, int color, boolean bound) {
        if (points.size() < 2) {
            Log.w(TAG, "drawLine: points is less than 2 ! size = " + points.size());
            return null;
        }
        ArrayList arrayList = new ArrayList(points.size());
        for (IGeoPoint iGeoPoint : points) {
            IGeoPoint baidu = iGeoPoint.toBaidu();
            arrayList.add(new LatLng(baidu.getLatitude(), baidu.getLongitude()));
        }
        try {
            Overlay addOverlayList = addOverlayList(type, this.mBaiduMap.addOverlay(new PolylineOptions().points(arrayList).color(color).width(10)));
            if (bound) {
                MapUtil.displayWithBound(this.mBaiduMap, arrayList);
            }
            return addOverlayList;
        } catch (Exception e) {
            Log.w(TAG, "drawLine: " + e.getMessage());
            return null;
        }
    }

    @Override
    public void drawLushu(final RouteBook routeBook, final int color, final boolean bound, final Runnable action) {
        this.subscriptions.add(Observable.just(routeBook).subscribeOn(Schedulers.io()).flatMap(lushu2 -> {
            if (lushu2 != null) {
                List<RouteBookPoint> byLushuId = RouteBookPoint.getByLushuId(lushu2.getId());
                List<Waypoint> byLushuId2 = Waypoint.getByLushuId(lushu2.getId());
                lushu2.setLushuPoints(byLushuId);
                lushu2.setWayPoints(byLushuId2);
            }
            return Observable.just(lushu2);
        }).observeOn(Schedulers.computation()).flatMap(lushu2 -> {
            List<RouteBookPoint> lushuPoints = lushu2.getLushuPoints();
            List<Waypoint> wayPoints = lushu2.getWayPoints();
            List<Notepoint> notepointList = lushu2.getNotepointList();
            ArrayList<List<LatLng>> arrayList = new ArrayList<>(3);
            ArrayList<LatLng> arrayList2 = new ArrayList<>(lushuPoints.size());
            ArrayList<LatLng> arrayList3 = new ArrayList<>(wayPoints.size());
            ArrayList<LatLng> arrayList4 = new ArrayList<>(notepointList.size());
            synchronized (BaiduMapFragment.this.converter) {
                for (RouteBookPoint lushuPoint : lushuPoints) {
                    arrayList2.add(BaiduMapFragment.this.earth2Baidu(lushuPoint.getLatLng()));
                }
                for (Waypoint waypoint : wayPoints) {
                    arrayList3.add(BaiduMapFragment.this.earth2Baidu(waypoint.getLatLng()));
                }
                for (Notepoint notepoint : notepointList) {
                    arrayList4.add(BaiduMapFragment.this.earth2Baidu(notepoint.getLatLng()));
                }
                arrayList.add(arrayList2);
                arrayList.add(arrayList3);
                arrayList.add(arrayList4);
            }
            return Observable.just(arrayList);
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(pointList -> {
            if (pointList == null) {
                return;
            }
            List<Overlay> drawSportLushu = MapUtil.drawSportLushu(BaiduMapFragment.this.mBaiduMap, routeBook, pointList.get(0), pointList.get(1), pointList.get(2), color, bound);
            if (drawSportLushu != null && !drawSportLushu.isEmpty()) {
                Bundle bundle = new Bundle();
                routeBook.setLushuPoints(null);
                routeBook.setWayPoints(null);
                bundle.putParcelable("lushu", routeBook);
                Overlay overlay = drawSportLushu.get(0);
                if (overlay != null) {
                    overlay.setExtraInfo(bundle);
                }
                synchronized (BaiduMapFragment.this.syncLock) {
                    BaiduMapFragment.this.lushuOverlays.addAll(drawSportLushu);
                }
            }
            if (action != null && BaiduMapFragment.this.handler != null) {
                BaiduMapFragment.this.handler.post(action);
            }
            if (BaiduMapFragment.this.getActivity() instanceof RouteBookDetailMapActivity routeBookDetailMapActivity) {
                routeBookDetailMapActivity.showAltitudeChart();
            }
        }));
    }

    @Override
    public void drawLushu(RouteBook routeBook, int color, boolean bound) {
        drawLushu(routeBook, color, bound, null);
    }

    @Override
    public List<Object> getOverlay(int type) {
        synchronized (this.syncLock) {
            return switch (type) {
                case 1 -> Arrays.asList(this.lushuOverlays.toArray());
                case 2 -> Arrays.asList(this.naviOverlays.toArray());
                case 3 -> Arrays.asList(this.workoutOverlays.toArray());
                case 4 -> Arrays.asList(this.altitudeOverlays.toArray());
                case 5 -> Arrays.asList(this.distanceOverlays.toArray());
                case 7 -> Arrays.asList(this.paceOverlays.toArray());
                case 8 -> Arrays.asList(this.segmentOverlays.toArray());
                case 9 -> Arrays.asList(this.poiOverlays.toArray());
                case 10 -> Arrays.asList(this.slopeOverlays.toArray());
                default -> Collections.emptyList();
            };
        }
    }

    @Override
    public Object drawMarker(int type, LatLng location, Drawable icon, String title, float anchorX, float anchorY) {
        BitmapDescriptor bitmapDescriptor;
        if (this.mBaiduMap == null) {
            return null;
        }
        MarkerOptions title2 = new MarkerOptions().position(location).anchor(anchorX, anchorY).title(title);
        if (icon != null && (bitmapDescriptor = getBitmapDescriptor(icon)) != null) {
            title2.icon(bitmapDescriptor);
        }
        return addOverlayList(type, this.mBaiduMap.addOverlay(title2));
    }

    @Override
    public Object drawMarker(int type, IGeoPoint location, Drawable icon, String title, float anchorX, float anchorY, int zIndex) {
        if (this.mBaiduMap == null) {
            return null;
        }
        IGeoPoint baidu = location.toBaidu();
        MarkerOptions title2 = new MarkerOptions().position(new LatLng(baidu.getLatitude(), baidu.getLongitude())).anchor(anchorX, anchorY).zIndex(zIndex).title(title);
        if (icon != null) {
            BitmapDescriptor bitmapDescriptor = getBitmapDescriptor(icon);
            if (bitmapDescriptor != null) {
                title2.icon(bitmapDescriptor);
            }
            return addOverlayList(type, this.mBaiduMap.addOverlay(title2));
        }
        return null;
    }

    private BitmapDescriptor getBitmapDescriptor(Drawable icon) {
        if (icon instanceof BitmapDrawable) {
            return BitmapDescriptorFactory.fromBitmap(((BitmapDrawable) icon).getBitmap());
        }
        if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
            Bitmap createBitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(createBitmap);
            canvas.drawColor(0);
            icon.draw(canvas);
            return BitmapDescriptorFactory.fromBitmap(createBitmap);
        }
        throw new IllegalStateException("icon not set width and height first !");
    }

    @Override
    public Object drawMarker(int type, IGeoPoint location, Drawable icon, String title, float anchorX, float anchorY) {
        return drawMarker(type, location, icon, title, anchorX, anchorY, 0);
    }

    private Overlay addOverlayList(int type, Overlay overlay) {
        synchronized (this.syncLock) {
            switch (type) {
                case OVERLAY_TYPE_ROAD_BOOK -> this.lushuOverlays.add(overlay);
                case OVERLAY_TYPE_NAV -> this.naviOverlays.add(overlay);
                case OVERLAY_TYPE_WORK_OUT -> this.workoutOverlays.add(overlay);
                case OVERLAY_TYPE_ALTITUDE -> this.altitudeOverlays.add(overlay);
                case OVERLAY_TYPE_DISTANCE -> this.distanceOverlays.add(overlay);
                case OVERLAY_TYPE_PACE -> this.paceOverlays.add(overlay);
                case OVERLAY_TYPE_SEGMENT -> this.segmentOverlays.add(overlay);
                case OVERLAY_TYPE_POI -> this.poiOverlays.add(overlay);
                case OVERLAY_TYPE_SLOPE_LEVEL -> this.slopeOverlays.add(overlay);
            }
        }
        return overlay;
    }

    @Override
    public synchronized boolean clearOverlay(int type) {
        boolean z;
        z = false;
        synchronized (this.syncLock) {
            switch (type) {
                case OVERLAY_TYPE_ROAD_BOOK:
                    Iterator<Overlay> it2 = this.lushuOverlays.iterator();
                    while (it2.hasNext()) {
                        removeOverlay(it2.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.lushuOverlays.clear();
                    break;
                case OVERLAY_TYPE_NAV:
                    Iterator<Overlay> it3 = this.naviOverlays.iterator();
                    while (it3.hasNext()) {
                        removeOverlay(it3.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.naviOverlays.clear();
                    break;
                case OVERLAY_TYPE_WORK_OUT:
                    Iterator<Overlay> it4 = this.workoutOverlays.iterator();
                    while (it4.hasNext()) {
                        removeOverlay(it4.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    Polyline polyline = this.sportLine;
                    if (polyline != null) {
                        removeOverlay(polyline);
                        this.sportLine = null;
                        if (!z) {
                            z = true;
                        }
                    }
                    this.workoutPoints.clear();
                    this.workoutOverlays.clear();
                    break;
                case OVERLAY_TYPE_ALTITUDE:
                    Iterator<Overlay> it5 = this.altitudeOverlays.iterator();
                    while (it5.hasNext()) {
                        removeOverlay(it5.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.altitudeOverlays.clear();
                    break;
                case OVERLAY_TYPE_DISTANCE:
                    Iterator<Overlay> it6 = this.distanceOverlays.iterator();
                    while (it6.hasNext()) {
                        removeOverlay(it6.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.distanceOverlays.clear();
                    break;
                case OVERLAY_TYPE_PACE:
                    Iterator<Overlay> it7 = this.paceOverlays.iterator();
                    while (it7.hasNext()) {
                        removeOverlay(it7.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.paceOverlays.clear();
                    break;
                case OVERLAY_TYPE_SEGMENT:
                    Iterator<Overlay> it8 = this.segmentOverlays.iterator();
                    while (it8.hasNext()) {
                        removeOverlay(it8.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.segmentOverlays.clear();
                    break;
                case OVERLAY_TYPE_POI:
                    Iterator<Overlay> it9 = this.poiOverlays.iterator();
                    while (it9.hasNext()) {
                        removeOverlay(it9.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.poiOverlays.clear();
                    break;
                case OVERLAY_TYPE_SLOPE_LEVEL:
                    Iterator<Overlay> it10 = this.slopeOverlays.iterator();
                    while (it10.hasNext()) {
                        removeOverlay(it10.next());
                        if (!z) {
                            z = true;
                        }
                    }
                    this.slopeOverlays.clear();
                    break;
            }
        }
        return z;
    }

    @Override
    public void removeOverlay(Object overlay, int type) {
        Overlay overlay2 = (Overlay) overlay;
        synchronized (this.syncLock) {
            switch (type) {
                case 1 -> this.lushuOverlays.remove(overlay);
                case 2 -> this.naviOverlays.remove(overlay);
                case 3 -> {
                    this.workoutOverlays.remove(overlay);
                    if (overlay.equals(this.sportLine)) {
                        this.sportLine = null;
                        this.workoutPoints.clear();
                    }
                }
                case 4 -> this.altitudeOverlays.remove(overlay2);
                case 5 -> this.distanceOverlays.remove(overlay2);
                case 7 -> this.paceOverlays.remove(overlay2);
                case 8 -> this.segmentOverlays.remove(overlay2);
                case 9 -> this.poiOverlays.remove(overlay2);
                case 10 -> this.slopeOverlays.remove(overlay2);
            }
        }
        overlay2.remove();
    }

    void removeOverlay(Overlay overlay) {
        if (overlay == null) {
            return;
        }
        try {
            overlay.remove();
        } catch (Exception e) {
            Log.w(TAG, "removeOverlay: " + e.getMessage());
        }
    }

    @Override
    public LatLng getCenter() {
        BaiduMap baiduMap;
        if (this.mapView == null || (baiduMap = this.mBaiduMap) == null || baiduMap.getProjection() == null) {
            return MapConfigs.getNotNullLastLocation();
        }
        return this.mBaiduMap.getProjection().fromScreenLocation(new Point(this.mapView.getWidth() / 2, this.mapView.getHeight() / 2));
    }

    @Override
    public void setLocationMode(int locationMode) {
        MyLocationConfiguration.LocationMode locationMode2;
        super.setLocationMode(locationMode);
        if (1 == locationMode) {
            locationMode2 = MyLocationConfiguration.LocationMode.NORMAL;
        } else if (2 == locationMode) {
            locationMode2 = MyLocationConfiguration.LocationMode.FOLLOWING;
        } else {
            locationMode2 = 3 == locationMode ? MyLocationConfiguration.LocationMode.COMPASS : null;
        }
        if (locationMode2 != null) {
            setLocationConfig(locationMode2);
        }
    }

    @Override
    public void setMapViewListener(MapViewListener mapViewListener) {
        this.mapViewListener = mapViewListener;
    }

    @Override
    public Object getProjection() {
        BaiduMap baiduMap = this.mBaiduMap;
        if (baiduMap == null) {
            return null;
        }
        return baiduMap.getProjection();
    }

    @Override
    protected GeoBounds getScreenRect() {
        Projection projection;
        BaiduMap baiduMap = this.mBaiduMap;
        if (baiduMap == null || this.mapView == null || (projection = baiduMap.getProjection()) == null) {
            return null;
        }
        Point point = new Point(0, 0);
        Point point2 = new Point(this.mapView.getWidth(), this.mapView.getHeight());
        LatLng fromScreenLocation = projection.fromScreenLocation(point);
        LatLng fromScreenLocation2 = projection.fromScreenLocation(point2);
        return new GeoBounds(2, fromScreenLocation.latitude, fromScreenLocation2.longitude, fromScreenLocation2.latitude, fromScreenLocation.longitude);
    }

    @Override
    public void displayBound(GeoBounds geoBounds) {
        if (this.mBaiduMap == null || this.mapView == null) {
            return;
        }
        GeoBounds type = geoBounds.toType(2);
        final ArrayList arrayList = new ArrayList(2);
        arrayList.add(new LatLng(type.north, type.east));
        arrayList.add(new LatLng(type.south, type.west));
        MapUtil.displayWithBound(this.mBaiduMap, arrayList);
        this.mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                MapUtil.displayWithBound(BaiduMapFragment.this.mBaiduMap, arrayList);
            }
        });
    }

    @Override
    protected boolean isPOISelected(MapPOI mapPOI) {
        MapPOI mapPOI2;
        Marker marker = this.currentMarker;
        if (marker == null || (mapPOI2 = (MapPOI) marker.getExtraInfo().getParcelable("map_poi")) == null) {
            return false;
        }
        return mapPOI2.equals(mapPOI);
    }

    @Override
    protected void setSelectedPOIMarker(Object marker) {
        if (marker != null && (marker instanceof Marker)) {
            this.currentMarker = (Marker) marker;
        } else {
            this.currentMarker = null;
        }
    }

    @Override
    protected void adjustCompassPosition(final int compassMargin) {
        App context = App.getContext();
        this.mBaiduMap.setCompassPosition(new Point(DensityUtil.dp2px(32.0f), Density.dp2px(context, compassMargin) + context.getResources().getDimensionPixelSize(R.dimen.status_bar_height)));
    }

    @Override
    public void hideMap(Context context, boolean isHideMap) {
        super.hideMap(context, isHideMap);
        if (isHideMap) {
            changeMapType(3);
            return;
        }
        int i = SharedManager.getInstance().getInt(MapConfigs.MULTI_MAP_TILE, 0);
        if (this.mBaiduMap != null) {
            changeMapType(i);
        }
    }

    public interface OnMapReadyListener {
        void OnMapReady(BaiduMapFragment newMap);
    }

    public class AnonymousClass7 implements BaiduMap.OnMapStatusChangeListener {
        LatLng target;
        float zoom;

        AnonymousClass7() {
            this.zoom = BaiduMapFragment.this.mBaiduMap.getMapStatus().zoom;
        }

        @Override
        public void onMapStatusChangeStart(MapStatus mapStatus) {
        }

        @Override
        public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
        }

        @Override
        public void onMapStatusChange(final MapStatus mapStatus) {
            BaiduMapFragment.this.runOnUIThread(() -> {
                if (BaiduMapFragment.this.touching) {
                    if (target == null || !target.equals(mapStatus.target)) {
                        target = mapStatus.target;
                        if (BaiduMapFragment.this.mapViewListener != null) {
                            BaiduMapFragment.this.mapViewListener.onDragMap(BaiduMapFragment.this.mapView, false);
                        }
                    }
                }
            });
        }

        @Override
        public void onMapStatusChangeFinish(final MapStatus mapStatus) {
            BaiduMapFragment.this.runOnUIThread(() -> {
                if (AnonymousClass7.this.zoom != mapStatus.zoom) {
                    if (!BaiduMapFragment.this.mClusterManager.getMarkerCollection().getMarkers().isEmpty()) {
                        BaiduMapFragment.this.mClusterManager.cluster();
                    }
                    if (BaiduMapFragment.this.mapViewListener != null) {
                        BaiduMapFragment.this.mapViewListener.onZoom(mapStatus.zoom);
                    }
                    BaiduMapFragment.this.flushPoi();
                    AnonymousClass7.this.zoom = mapStatus.zoom;
                }
                if (BaiduMapFragment.this.touching) {
                    BaiduMapFragment.this.touching = false;
                    BaiduMapFragment.this.flushPoi();
                    if (BaiduMapFragment.this.mapViewListener != null) {
                        BaiduMapFragment.this.mapViewListener.onDragMap(BaiduMapFragment.this.mapView, true);
                    }
                }
            });
        }
    }

    public class TeamItem implements ClusterItem {
        LatestLocation latestLocation;
        LatLng mPosition;

        private TeamItem(LatestLocation position) {
            this.latestLocation = position;
            this.mPosition = new LatLng(position.getLatitude(), position.getLongitude());
        }

        @Override
        public LatLng getPosition() {
            return this.mPosition;
        }

        @Override
        public BitmapDescriptor getBitmapDescriptor() {
            return null;
        }
    }
}